import 'package:flutter/material.dart';
import 'package:x3_base_core/base_models/base_key_value_model.dart';
import 'package:x3_component_core/components/base_components/base_filter_select_chip.dart';
import 'package:x3_component_core/components/selector_components/base_filter_selector.dart';
import 'package:x3_component_core/models/base_advanced_filter_model.dart';

class BaseFilterRadio extends StatefulWidget {
  final AdvancedFilterModel model;

  final Map<String, dynamic> dataSource;

  const BaseFilterRadio({Key key, @required this.model, @required this.dataSource})
      : assert(model != null),
        super(key: key);

  @override
  _BaseFilterRadioState createState() => _BaseFilterRadioState();
}

class _BaseFilterRadioState extends State<BaseFilterRadio> with AutomaticKeepAliveClientMixin {
  AdvancedFilterModel get model => widget.model;

  Map<String, dynamic> get _dataSource => widget.dataSource;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    String _selectedText = model.tags.firstWhere((element) => element.key == _dataSource[model.tagId]).value;
    List<Widget> children = [];
    for (BaseKeyValueModel e in model.tags) {
      bool selected = e.key == _dataSource[model.tagId];
      children.add(
        BaseFilterSelectChip(
          selected: selected,
          label: e.value,
          onSelected: (bool) {
            _dataSource[model.tagId] = e.key;
            setState(() {});
          },
        ),
      );
    }
    return BaseFilterSelector(
      title: model.tagName,
      selectedText: _selectedText,
      isExpanded: model.tagIsExpanded,
      children: children,
      onChange: (bool state) {
        model.tagIsExpanded = state;
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
